package com.mayuhao.loginmanage.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mayuhao.loginmanage.entity.CssMenu;
import com.mayuhao.loginmanage.entity.CssUserRole;
import com.mayuhao.loginmanage.entity.CssUserRoleVO;
import com.mayuhao.loginmanage.mapper.CssUserRoleMapper;
import com.mayuhao.loginmanage.service.ICssUserRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 马宇豪
 * @since 2022-10-23
 */
@Service
public class CssUserRoleServiceImpl extends ServiceImpl<CssUserRoleMapper, CssUserRole> implements ICssUserRoleService {

    @Resource
    private CssUserRoleMapper cssUserRoleMapper;

    /**
     * @return com.mayuhao.loginmanage.entity.CssUserRoleVO
     * @description 请修改方法描述
     * @methodName login
     * @params [userRoleVO]
     * @paramType [com.mayuhao.loginmanage.entity.CssUserRoleVO]
     * @author 马宇豪
     * @date 2022/10/28 0028 下午 11:45
     * @email 1757442521@qq.com
     **/
    @Override
    public CssUserRoleVO login(CssUserRoleVO userRoleVO) {
        String userId = userRoleVO.getUserId();
        String userPassword = userRoleVO.getUserPassword();
        String roleId = userRoleVO.getRoleId();
//    通过角色id  确定此人是何角色  在相应表中查询是否有此人存在 并校验密码
        if ("1".equals(roleId)) {
            userRoleVO = cssUserRoleMapper.studentLogin(userId, userPassword);
            if (userRoleVO == null) {
                return null;
            }
            System.err.println(userRoleVO);
        } else if ("2".equals(roleId)) {
            userRoleVO = cssUserRoleMapper.teacherLogin(userId, userPassword);
            if (userRoleVO == null) {
                return null;
            }
            System.err.println(userRoleVO);
        } else {
            userRoleVO = cssUserRoleMapper.adminLogin(userId, userPassword);
            if (userRoleVO == null) {
                return null;
            }
            System.err.println(userRoleVO);
        }
        List<CssMenu> menus = cssUserRoleMapper.getMenus(roleId);
        userRoleVO.setMenus(menus);
        userRoleVO.setRoleId(roleId);
        return userRoleVO;
//        System.err.println(userRoleVO);
    }

    /**
     * @return boolean
     * @description 请修改方法描述
     * @methodName changePassword
     * @params [newpassword]
     * @paramType [java.lang.String]
     * @author 马宇豪
     * @date 2022/11/12 0012 下午 2:15
     * @email 1757442521@qq.com
     **/
    @Override
    public boolean changeStudentPassword(String studentId, String newPassword) {
        return cssUserRoleMapper.changeStudentPassword(studentId, newPassword);
    }

    /**
     * @return boolean
     * @description 请修改方法描述
     * @methodName changeTeacherPassword
     * @params [teacherId, newPassword]
     * @paramType [java.lang.String, java.lang.String]
     * @author 马宇豪
     * @date 2022/11/12 0012 下午 2:50
     * @email 1757442521@qq.com
     **/
    @Override
    public boolean changeTeacherPassword(String teacherId, String newPassword) {
        return cssUserRoleMapper.changeTeacherPassword(teacherId, newPassword);
    }

    /**
     * @return boolean
     * @description 请修改方法描述
     * @methodName changeAdminPassword
     * @params [adminId, newPassword]
     * @paramType [java.lang.String, java.lang.String]
     * @author 马宇豪
     * @date 2022/11/12 0012 下午 2:56
     * @email 1757442521@qq.com
     **/
    @Override
    public boolean changeAdminPassword(String adminId, String newPassword) {
        return cssUserRoleMapper.changeAdminPassword(adminId, newPassword);
    }
}
